<template>
  <div class="app-container">
    <div>
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <el-card>
            <div slot="header">
              <span>个人信息</span>
            </div>
            <div style="text-align: center">
              <el-upload
                class="avatar-uploader"
                :action="uploadUrl"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </div>
            <el-descriptions class="margin-top":column="1" border>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  登录名称
                </template>
                {{sysUser.userName}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  用户昵称
                </template>
                {{sysUser.nickName}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user"></i>
                  直接领导
                </template>
                {{sysUser.leaderName}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-mobile-phone"></i>
                  手机号码
                </template>
                {{ sysUser.phone }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-suitcase"></i>
                  用户邮箱
                </template>
                {{ sysUser.email }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-user-solid"></i>
                  所属部门
                </template>
                {{sysUser.deptName}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-time"></i>
                  注册时间
                </template>
                {{sysUser.createTime}}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-time"></i>
                  最后登录时间
                </template>
                {{sysUser.loginDate}}
              </el-descriptions-item>
            </el-descriptions>
          </el-card>
        </el-col>
        <el-col :span="18" :xs="24">
          <el-card>
            <div slot="header">
              <span>基本资料</span>
            </div>
            <el-tabs v-model="activeTab">
              <el-tab-pane label="基本资料" name="userInfo">
                <user-info @after="getUserInfo"></user-info>
              </el-tab-pane>
              <el-tab-pane label="修改密码" name="updatePwd">
                <update-pwd></update-pwd>
              </el-tab-pane>
            </el-tabs>
          </el-card>
        </el-col>

      </el-row>
    </div>
  </div>
</template>

<script>
import {getInfo} from "@/api/user";
import updatePwd from "./components/update-pwd"
import userInfo from "./components/user-info"
import store from "@/store";

export default {
  name: 'Profile',
  components:{
    updatePwd,
    userInfo
  },
  data() {
    return {
      user: {},
      activeTab: 'userInfo',
      imageUrl: '',
      sysUser:{},
      uploadUrl:process.env.VUE_APP_BASE_API+"/sysUser/uploadUserAvatar"
    }
  },
  created() {
      this.getUserInfo();
  },
  methods: {
    //获取当前用户数据
    getUserInfo(){
      getInfo().then(res=>{
        this.sysUser=res.data.user;
        this.imageUrl=this.sysUser.avatar
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.$store.dispatch('user/getInfo')
    },
    beforeAvatarUpload(file) {
      console.log(file.type)
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isPNG) {
        this.$message.error('只能上传PNG的格式');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isPNG && isLt2M;
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
